package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;

public class ContactCreationTests {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @BeforeClass
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @org.testng.annotations.Test
  public void testContactCreation() throws Exception {
    openMainPage();
    openContacPageCreation();
    ContactData contact = new ContactData();
    contact.contactname = "contactname";
    contact.lastname = "lastname";
    contact.address = "adddress";
    contact.TelephoneHome = "1111";
    contact.TelephoneMobile = "2222";
    contact.TelephoneWork = "3333";
    contact.EmailOne = "mailgmailcom";
    contact.EmailSecond = "secondemailgmailcom";
    contact.BirthDay = "4";
    contact.BirthMonth = "July";
    contact.BirthYear = "1981";
	contact.GroupNane = "Rob";
	contact.SecondaryAddress = "againaddress";
	contact.SecondaryHome = "againhome";
    fillContactForm(contact);
    submitContactForm();
    openHomePage(); 
  }
  
  @org.testng.annotations.Test
  public void testEmptyContactCreation() throws Exception {
	    openMainPage();
	    openContacPageCreation();
	    ContactData newcontat = new ContactData();
	    newcontat.contactname = "Newcontactname";
	    newcontat.lastname = "newlastname";
	    newcontat.address = "newaddress";
	    newcontat.TelephoneHome = "123";
	    newcontat.TelephoneMobile = "234";
	    newcontat.TelephoneWork = "345";
	    newcontat.EmailOne = "maildghj@gmail.com";
	    newcontat.EmailSecond = "secondnewemail@ukr.net";
	    newcontat.BirthDay = "3";
	    newcontat.BirthMonth = "January";
	    newcontat.BirthYear = "1989";
	    newcontat.GroupNane = "Rob";
	    newcontat.SecondaryAddress = "mynewadress";
	    newcontat.SecondaryHome = "newmyhome";
	    fillContactForm(newcontat);
	    submitContactForm();
	    openHomePage();
	  }

  /*@org.testng.annotations.Test
  public void testEmptyContactCreation() throws Exception {
	    openMainPage();
	    openContacPageCreation();
	    fillContactForm(new ContactData("contactname", "lastame", "address", "1111", "2222", "3333", "mailgmailcom", "secondemailgmailcom", "4", "F", "1984", "Rob", "againaddress", "againhome"));
	    submitContactForm();
	    openHomePage();
	*/    
  @org.testng.annotations.Test
  public void testThreeContactCreation() throws Exception {
	    openMainPage();
	    openContacPageCreation();
	    ContactData newcontat = new ContactData();
	    newcontat.contactname = "Newcontactname";
	    newcontat.lastname = "newlastname";
	    newcontat.address = "newaddress";
	    newcontat.TelephoneHome = "123";
	    newcontat.TelephoneMobile = "234";
	    newcontat.TelephoneWork = "345";
	    newcontat.EmailOne = "maildghj@gmail.com";
	    newcontat.EmailSecond = "secondnewemail@ukr.net";
	    newcontat.BirthDay = "3";
	    newcontat.BirthMonth = "January";
	    newcontat.BirthYear = "1989";
	    newcontat.GroupNane = "Rob";
	    newcontat.SecondaryAddress = "mynewadress";
	    newcontat.SecondaryHome = "newmyhome";
	    fillContactForm(newcontat);
	    submitContactForm();
	    openHomePage();
	  }
  
private void openHomePage() {
	driver.findElement(By.linkText("home page")).click();
}

private void submitContactForm() {
	driver.findElement(By.name("submit")).click();
}

private void fillContactForm(ContactData contat) {
	driver.findElement(By.name("firstname")).clear();
    driver.findElement(By.name("firstname")).sendKeys(contat.contactname);
    driver.findElement(By.name("lastname")).clear();
    driver.findElement(By.name("lastname")).sendKeys(contat.lastname);
    driver.findElement(By.name("address")).clear();
    driver.findElement(By.name("address")).sendKeys(contat.address);
    driver.findElement(By.name("home")).clear();
    driver.findElement(By.name("home")).sendKeys(contat.TelephoneHome);
    driver.findElement(By.name("mobile")).clear();
    driver.findElement(By.name("mobile")).sendKeys(contat.TelephoneMobile);
    driver.findElement(By.name("work")).clear();
    driver.findElement(By.name("work")).sendKeys(contat.TelephoneWork);
    driver.findElement(By.name("email")).clear();
    driver.findElement(By.name("email")).sendKeys(contat.EmailOne);
    driver.findElement(By.name("email2")).clear();
    driver.findElement(By.name("email2")).sendKeys(contat.EmailSecond);
    
    new Select(driver.findElement(By.name("bday"))).selectByVisibleText(contat.BirthDay);
    new Select(driver.findElement(By.name("bmonth"))).selectByVisibleText(contat.BirthMonth);
    
    driver.findElement(By.name("byear")).clear();
    driver.findElement(By.name("byear")).sendKeys(contat.BirthYear);
    
    new Select(driver.findElement(By.name("new_group"))).selectByVisibleText(contat.GroupNane);
    driver.findElement(By.xpath("//div[@id='content']/form/select[3]/option[10]")).click();
    new Select(driver.findElement(By.name("new_group"))).selectByVisibleText("group name");
    driver.findElement(By.name("address2")).clear();
    driver.findElement(By.name("address2")).sendKeys(contat.SecondaryAddress);
    driver.findElement(By.name("phone2")).clear();
    driver.findElement(By.name("phone2")).sendKeys(contat.SecondaryHome);
}

private void openContacPageCreation() {
	driver.findElement(By.linkText("add new")).click();
}

private void openMainPage() {
	driver.get(baseUrl + "/addressbookv4.1.4/");
}

  @AfterClass
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
